lobsterh5.main({
    data: {
        patientlist: [],
        selectPatient: null,
        lisResult: [], //检验
        pacsResult: [],//检查
        startDate: "",
        endDate: "",
        filterIndex: 0,
        // currentbranch: {},//当前分院
    },
    pageload() {
        var self = this;
        // self.data.currentbranch = JSON.parse(lobsterh5.GetAppStorage("selectbranch")) || {};
        self.data.startDate = self.myGetDay(7);
        self.data.endDate = self.myGetDay(0);
        self.initdata();
        self.initevent();
    },
    myGetDay: function (days) {
        var self = this;
        var time = (new Date).getTime() - 24 * 60 * 60 * 1000 * days;
        var date = new Date(time); // 获取的是前几天日期
        date = self.formartdate(date);
        return date;
    },
    formartdate(date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        var d = date.getDate();
        d = d < 10 ? '0' + d : d;
        return y + '-' + m + '-' + d;
    },
    initdata() {
        var self = this;
        lobsterh5.GET("/admin/v1/CPatient/GetUserPatient").then(res => {
            self.data.patientlist = res.patdata;
            // self.getdata();
        }).catch(res => {
            $.toast(res.msg, 'text');
        });
    },
    initevent() {
        var self = this;

        $(document).on("click", "#patiengdiv", function () {
            $("#half").popup();
            self.inittemp("tplcard", "sidebaritem", { list: self.data.patientlist });
        })

        //选定就诊人
        $(document).on("click", ".carditem", function (obj) {
            var card = obj.currentTarget.dataset.info;
            var info = JSON.parse(card);
            lobsterh5.GET("/admin/v1/CPatient/GetCardTypeListByPId", { patientId: info.PatientId }).then(res => {
                var cardList = res.cardList;//已绑卡
                var cardTypeList = res.cardTypeList;//卡类型字典
                //先找诊疗卡，未绑定诊疗卡则找身份证
                var arr = cardList.filter(x => x.Code == '1');
                if (arr.length > 0) {
                    info.CardType = 1;//诊疗卡
                    info.CardNum = arr[0].CardNo;
                } else {
                    info.CardType = 2;//身份证
                    info.CardNum = info.IDCard;
                }

                self.data.selectPatient = info;
                //测试就诊人 （身份证：320***********9739）
                $("#selectedPatient").html(info.Name + "  (" + info.IDCard + " )");
                $.closePopup()
                $("#div_data").html("");

                self.getdata();

            }).catch(res => {
                $.toast(res.msg, 'forbidden');
            });

        });

        $('#t1').tab({
            defaultIndex: 0,
            activeClass: 'tab-green',
            onToggle: function (index) {
                console.log('index' + index);
                self.data.filterIndex = index;
                if (self.data.selectPatient) {
                    self.getdata();
                }

            }
        });
        $(document).on("click", ".labeltag", function (obj) {
            var index = $(this).data("index");
            $(".labeltag").removeClass("selectLable").addClass('f-gray');
            $(this).addClass('selectLable').removeClass('f-gray');

            self.filterdata(index);
            if (self.data.selectPatient) {
                self.getdata();
            }
        })

        $(document).on("click", ".item", function (obj) {
            console.log(obj.currentTarget.dataset.info);
            var info = JSON.parse(obj.currentTarget.dataset.info);
            if (self.data.filterIndex == 0) {
                let url = 'lisdetail.html?appid' + lobsterh5.GetAppId();
                localStorage.setItem("lisitemdata", JSON.stringify(info));
                location.href = url;
            } else if (self.data.filterIndex == 1) {
                let url = 'pacsdetail.html?appid' + lobsterh5.GetAppId();
                localStorage.setItem("pacsitemdata", JSON.stringify(info));
                location.href = url;
            }
        });

        //下拉加载数据
        $('.container').infinite(100).on("infinite", function () {

        });

    },
    filterdata(index) {
        var self = this;
        let day = 7;
        switch (index) {
            case 0:
                day = 7;
                break;
            case 1:
                day = 30;
                break;
            case 2:
                day = 180;
                break;
            case 3:
                day = -1;
                break;
        }
        var end = self.formartdate(new Date());
        var start = self.myGetDay(day);
        if (day == -1) {
            start = "2000-01-01";
        }
        self.data.startDate = start;
        self.data.endDate = end;
        console.log(end + '_' + start);
        // self.getdata();
    },
    getdata() {
        var self = this;
        // $("#more").html('<i class="weui-loading"></i><span class="weui-loadmore__tips">正在加载</span>');
        // $("#more").show();
        $.showLoading();
        if (self.data.filterIndex == 0) {
            self.initlisData(function () {
                // $("#more").html("没有更多数据了");
                $.hideLoading();
            });
        } else if (self.data.filterIndex == 1) {
            self.initpcaData(function () {
                // $("#more").html("没有更多数据了");
                $.hideLoading();
            });
        }
    },


    getlisData(fun) {
        var self = this;
        var data = {
            CardType: self.data.selectPatient.CardType,
            CardNum: self.data.selectPatient.CardNum,
            StarDate: self.data.startDate,
            EndDate: self.data.endDate,
        };
        lobsterh5.GETAPI("tsreport.getLisResult", data).then(res => {
            console.log(res);
            if (fun) {
                fun(res)
            }
        }).catch(res => {
            $.toptip(res.msg, 'warning');
        })

    },
    //检验
    initlisData(fun) {
        var self = this;
        // $(".weui-loadmore").show()
        self.getlisData(function (res) {
            // $(".weui-loadmore").hide();
            fun && fun();
            var lisResult = res.Items;
            self.data.lisResult = lisResult;
            self.inittemp("tplrecord", "div_data", { list: lisResult });
        })
    },

    getpcaData(fun) {
        var self = this;
        var data = {
            CardType: self.data.selectPatient.CardType,
            CardNum: self.data.selectPatient.CardNum,
            StarDate: self.data.startDate,
            EndDate: self.data.endDate,
        };
        lobsterh5.GETAPI("tsreport.getPacsResult", data).then(res => {
            console.log(res);
            if (fun) {
                fun(res)
            }
        }).catch(res => {
            $.toptip(res.msg, 'warning');
        })
    },
    //检查
    initpcaData(fun) {
        var self = this;
        // $(".weui-loadmore").show();
        self.getpcaData(function (res) {
            // $(".weui-loadmore").hide();
            fun && fun();

            var pacsResult = res.Items;

            self.data.pacsResult = pacsResult;
            self.inittemp("tplrecord", "div_data", { list: pacsResult });
        });

    },
    /**
     * 项目分组
     * @param {*} array 
     * @param {*} f 
     */
    groupBy: function (array, f) {
        let groups = {};
        array.forEach(function (o) {
            let group = JSON.stringify(f(o));
            groups[group] = groups[group] || [];
            groups[group].push(o);
        });
        return Object.keys(groups).map(function (group) {
            return groups[group];
        });
    },
    /**
     * 排序
     * @param {*} array 
     * @param {*} key 
     */
    sortKey(array, key) {
        return array.sort(function (a, b) {
            var x = a[key];
            var y = b[key];
            return x > y ? -1 : x < y ? 1 : 0;
        });
    },
    inittemp(tempid, _id, data) {
        var html = template(tempid, data);
        $("#" + _id).html(html);
    },
});